package com.zsy.srpingframework.resourceloader.loader;

import cn.hutool.core.lang.Assert;
import com.zsy.srpingframework.resourceloader.Resource;
import com.zsy.srpingframework.resourceloader.ResourceLoader;
import com.zsy.srpingframework.resourceloader.resource.ClassPathResource;
import com.zsy.srpingframework.resourceloader.resource.FileSystemResource;
import com.zsy.srpingframework.resourceloader.resource.UrlResoucre;

import java.net.MalformedURLException;
import java.net.URL;

public class DefaultResourceLoader implements ResourceLoader {
    //需要判断是哪种资源然后get
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "location must not be null");
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        } else {
            URL url = null;
            try {
                url = new URL(location);
                return new UrlResoucre(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return new FileSystemResource(location);
            }
        }
    }
}
